package edu.sandau.b230111.b23011118.controller;

import edu.sandau.b230111.b23011118.entity.Klass;
import edu.sandau.b230111.b23011118.service.KlassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("klass")
public class KlassController {
    @Autowired
    private KlassService klassService;

    @RequestMapping("/list")
    public String list(ModelMap map, Klass klass) {
        if ("".equals(klass.getName())) {
            klass.setName(null);
        }
        map.put("stu1", klassService.findAll(klass));//从数据库取数据
        map.put("currentMenu","class");
        return "klass/list";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        klassService.deleteById(id);
        return "redirect:/klass/list";
    }

    @RequestMapping("create")
    public String create() {
        return "/klass/create";
    }

    @RequestMapping("save")
    public String save(Klass s) {
        if (s.getId() != null)
            klassService.update(s);
        else
            klassService.save(s);
        return "redirect:/klass/list";
    }

    @RequestMapping("update/{id}")
    public String update(@PathVariable Integer id, ModelMap map) {
        Klass s = klassService.getReferenceById(id);
        map.put("klass", s);
        return "klass/update";
    }

}


